package com.wyj.rpc.service.impl;

import com.wyj.rpc.anno.RpcService;
import com.wyj.rpc.pojo.Order;
import com.wyj.rpc.pojo.User;
import com.wyj.rpc.service.IOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.*;

@Service
@RpcService
public class OrderServiceImpl implements IOrderService {
    private final static Map<Integer, Order> ORDER_MAP = new HashMap<>(8);

    @PostConstruct
    public void init() {
        ORDER_MAP.put(1, new Order(1, 1, "北京市朝阳区", LocalDateTime.now()));
        ORDER_MAP.put(2, new Order(2, 2, "北京市海淀区", LocalDateTime.now()));
        ORDER_MAP.put(3, new Order(3, 3, "北京市西城区", LocalDateTime.now()));
        ORDER_MAP.put(4, new Order(4, 4, "北京市丰台区", LocalDateTime.now()));

        ORDER_MAP.put(5, new Order(5, 1, "北京市朝阳区", LocalDateTime.now()));
        ORDER_MAP.put(6, new Order(6, 2, "北京市海淀区", LocalDateTime.now()));
        ORDER_MAP.put(7, new Order(7, 3, "北京市西城区", LocalDateTime.now()));
        ORDER_MAP.put(8, new Order(8, 4, "北京市丰台区", LocalDateTime.now()));

        ORDER_MAP.put(9, new Order(9, 1, "北京市朝阳区", LocalDateTime.now()));
        ORDER_MAP.put(10, new Order(10, 2, "北京市海淀区", LocalDateTime.now()));
        ORDER_MAP.put(11, new Order(11, 3, "北京市西城区", LocalDateTime.now()));
        ORDER_MAP.put(12, new Order(12, 4, "北京市丰台区", LocalDateTime.now()));

        ORDER_MAP.put(13, new Order(13, 1, "北京市朝阳区", LocalDateTime.now()));
        ORDER_MAP.put(14, new Order(14, 2, "北京市海淀区", LocalDateTime.now()));
        ORDER_MAP.put(15, new Order(15, 3, "北京市西城区", LocalDateTime.now()));
        ORDER_MAP.put(16, new Order(16, 4, "北京市丰台区", LocalDateTime.now()));
    }

    /**
     * TODO: 根据订单id查询
     *
     * @param id 主键
     * @return com.wyj.rpc.pojo.Order
     * @author wuyijie
     * @date 2021/12/26 5:19 下午
     */
    @Override
    public Order findById(Integer id) {
        return ORDER_MAP.get(id);
    }

    /**
     * TODO: 根据用户id查询
     *
     * @param userId 用户主键
     * @return java.util.List<com.wyj.rpc.pojo.Order>
     * @author wuyijie
     * @date 2021/12/26 5:20 下午
     */
    @Override
    public List<Order> findListByUserId(Integer userId) {
        List<Order> list = new ArrayList<>();
        Set<Map.Entry<Integer, Order>> entries = ORDER_MAP.entrySet();
        for (Map.Entry<Integer, Order> entry : entries) {
            if (userId.equals(entry.getValue().getUserId())) {
                list.add(entry.getValue());
            }
        }
        return list;
    }
}
